package cn.swing.his.finance.webapi.service.impl;

import cn.swing.his.common.exception.HisServiceException;
import cn.swing.his.common.web.JsonPage;
import cn.swing.his.common.web.ResponseCode;
import cn.swing.his.finance.service.ISalaryService;
import cn.swing.his.finance.webapi.mapper.SalaryMapper;
import cn.swing.his.pojo.finance.dto.AddFinanceDetailDTO;
import cn.swing.his.pojo.finance.dto.AddSalaryDTO;
import cn.swing.his.pojo.finance.dto.TimeDTO;
import cn.swing.his.pojo.finance.entity.FinanceDetail;
import cn.swing.his.pojo.finance.entity.Salary;
import cn.swing.his.pojo.finance.vo.SalaryDetailVO;
import cn.swing.his.pojo.finance.vo.SalaryVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SalaryServiceImpl implements ISalaryService {
    @Autowired
    private SalaryMapper salaryMapper;
    @Autowired
    private FinanceDetailServiceImpl financeDetailService;

    @Override
    public JsonPage<SalaryVO> salaryList(Integer pageNum, Integer pageSize,Integer index) {
        PageHelper.startPage(pageNum,pageSize);
        List<SalaryVO> list;
        if (index==0){
            list= salaryMapper.salaryList();
        }else{
            list= salaryMapper.salaryListByState(index-1);
        }
        return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public void updateSalaryState(Integer id) {
//        发放工资时候先查询改id工资是否发放
        Integer state=salaryMapper.getSalaryStatById(id);
        if(state==1){
            throw new HisServiceException(ResponseCode.BAD_REQUEST,"该用户工资已经发放请勿重复操作！");
        }
        int rows=salaryMapper.updateState(id,1);
        if (rows==0){
            throw new HisServiceException(ResponseCode.BAD_REQUEST,"服务器异常");
        }
        /*要将发放时间修改为当前时间*/
        LocalDateTime deliveryTime=LocalDateTime.now();
        AddFinanceDetailDTO addFinanceDetailDTO=new AddFinanceDetailDTO();
        SalaryDetailVO salaryDetailVO=salaryMapper.getSalaryDetailById(id);
        log.debug("salaryDetailVO:{}",salaryDetailVO);
        BeanUtils.copyProperties(salaryDetailVO,addFinanceDetailDTO);
        log.debug("addFinanceDetailDTO:{}",addFinanceDetailDTO);
        addFinanceDetailDTO.setType(1);
        addFinanceDetailDTO.setPrice(salaryDetailVO.getSalaryDetails());
        financeDetailService.insertFinanceDetail(addFinanceDetailDTO);
        salaryMapper.updateDeliveryTime(id,deliveryTime);
    }

    @Override
    public void updateBatchSalaryState(Integer id) {
        //        发放工资时候先查询改id工资是否发放
        Integer state=salaryMapper.getSalaryStatById(id);
        if(state==0){
            int rows=salaryMapper.updateState(id,1);
            if (rows==1){
                /*要将发放时间修改为当前时间*/
                LocalDateTime deliveryTime=LocalDateTime.now();
                AddFinanceDetailDTO addFinanceDetailDTO=new AddFinanceDetailDTO();
                SalaryDetailVO salaryDetailVO=salaryMapper.getSalaryDetailById(id);
                log.debug("salaryDetailVO:{}",salaryDetailVO);
                BeanUtils.copyProperties(salaryDetailVO,addFinanceDetailDTO);
                log.debug("addFinanceDetailDTO:{}",addFinanceDetailDTO);
                addFinanceDetailDTO.setType(1);
                addFinanceDetailDTO.setPrice(salaryDetailVO.getSalaryDetails());
                financeDetailService.insertFinanceDetail(addFinanceDetailDTO);
                salaryMapper.updateDeliveryTime(id,deliveryTime);
            }
        }
    }

    @Override
    public List<SalaryVO> salaryAllList(TimeDTO time) {
        List<SalaryVO> list=salaryMapper.salaryAllList(time.getStartDate(),time.getEndDate());
        for (SalaryVO s:list ) {
            s.setState("0".equals(s.getState())?"未发放":"已发放");
        }
        return list;
    }

    @Override
    public void insertSalary(AddSalaryDTO addSalaryDTO) {
//
        Integer doctorId = salaryMapper.getDoctorIdByName(addSalaryDTO.getDoctorName());
        if (doctorId==null){
            throw new HisServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"该医生不存在，请重新输入或者与相关部门核实！");
        }
        salaryMapper.insertSalary(doctorId,addSalaryDTO.getSalaryDetails(),addSalaryDTO.getDescription());
    }
}
